/* components */
import { BaseSelectUserRoleItem } from '@src/component/common/BaseSelectUser/components'
/* enum */
import { BaseSelectUserComponentNameEnum, SelectUserComponentRole } from '@src/component/common/BaseSelectUser/model'
/* vue */
// @ts-ignore
import { ComponentInstance, ComponentRenderProxy, defineComponent, PropType } from 'vue'
import { CommonComponentInstance } from '@model/VC'
import { CreateElement } from 'vue'

export type RoleProps = {
  isMulti: boolean;
  value: SelectUserComponentRole[];
}

export interface RoleSetupState {
  
}

export enum RoleEmitEventNameEnum {
  Input = 'input',
  Click = 'click',
}

export type RoleInstance = ComponentInstance & RoleSetupState
export type RoleVM = ComponentRenderProxy<RoleProps> & CommonComponentInstance & RoleInstance

export default defineComponent({
  name: BaseSelectUserComponentNameEnum.BaseSelectUserRole,
  emits: [
    RoleEmitEventNameEnum.Input,
    RoleEmitEventNameEnum.Click
  ],
  props: {
    /* 是否为多选状态 */
    isMulti: {
      type: Boolean as PropType<boolean>,
      default: false
    },
    value: {
      type: Array as PropType<SelectUserComponentRole[]>,
      default: () => []
    }
  },
  setup(props: RoleProps, { emit }) {
    
    const onClickHandler = (value: SelectUserComponentRole) => {
      emit(RoleEmitEventNameEnum.Click, value)
    }
    
    return {
      onClickHandler
    }
    
  },
  render(h: CreateElement) {
    return (
      <div class={BaseSelectUserComponentNameEnum.BaseSelectUserRole}>
        
        {this.value.map(role => {
          return (
            <BaseSelectUserRoleItem 
              value={role} 
              key={role.id}
              is-multi={this.isMulti} 
              onClick={this.onClickHandler}
            />
          )
        })}
        
      </div>
    )
  }
})
